package com.kfpanda.search.index.file;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.kfpanda.search.index.AbstractFileDocument;

/**
 * <b>ClassName:<b> TextDocument
 * <br><b>Description: <br>
 * 将文本文件转化成Document。
 * @Author: kfpanda
 * @Date：2012-1-8 下午03:48:45
 * @ModifyUser：shapecool
 *
 */
public class TextDocument extends AbstractFileDocument {
	
	private Logger logger = Logger.getLogger(TextDocument.class);
	
	/**
	 * <b>Description:<b><br>
	 * 获得指定行数的文本文件内容。
	 * @param fileName
	 * @param charset
	 * @param rowNum
	 * @return
	 * @throws IOException
	 * @Author: kfpanda
	 * @ModifyUser：kfpanda
	 * @Date：2012-1-8 下午03:59:44
	 */
	public String fileReaderAll(String filePath, String charset,
			int rowNum) {
		BufferedReader reader = null;
		String stemp = new String();
		try {
			reader = new BufferedReader(new InputStreamReader(
							new FileInputStream(filePath), charset));
			String line = new String();
			int i = 0;
			while ((line = reader.readLine()) != null && i++ < rowNum) {
				stemp += line;
			}
			reader.close();
		} catch (UnsupportedEncodingException e) {
			logger.error("文件编码格式（" + charset + "）不支持", e);
		} catch (FileNotFoundException e) {
			logger.error("文件（" + filePath + "）无法找到", e);
		} catch (IOException e) {
			logger.error("文件（" + filePath + "）读取失败", e);
		} finally{
			IOUtils.closeQuietly(reader);
		}
		return stemp;
	}
	
}
